﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Globalization;

namespace FSY.API.Utilities
{
    public class FsyLoggerHelper
    {
        public static void WriteError(string errorMessage)
        {
            try
            {
                string path = Files.Fsy_FileController.GetVitualFilePath("Logs/" + string.Format("{0:yyyy-MM-dd}",DateTime.Now) + ".txt");
                if (!File.Exists(HttpContext.Current.Server.MapPath(path)))
                {
                    File.Create(HttpContext.Current.Server.MapPath(path)).Close();
                }                
                using (StreamWriter w = File.AppendText(HttpContext.Current.Server.MapPath(path)))
                {
                    w.WriteLine("\r\nLog Entry : ");
                    w.WriteLine("{0:yyyy-MM-dd}({0:T})", DateTime.Now);
                    string err = "Error in: " + HttpContext.Current.Request.Url.ToString() +
                                  ". Error Message :  " + errorMessage;
                    w.WriteLine(err);
                    w.WriteLine("__________________________");
                    w.Flush();
                    w.Close();
                }
            }
            catch (FileNotFoundException ex)
            {
                WriteError(ex.Message);
            }
        }
    }
}
